package com.wzqj.agent.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.wzqj.agent.common.ConfigHelper;
import com.wzqj.wsp.response.ResponseValue;
import com.wzqj.wsp.util.BaiduUtil;

/**
 * 地图Controller
 * @author gz
 *
 */
@Controller
@RequestMapping("/map")
public class MapController {

	private static final String BAIDU_KEY = ConfigHelper.getProperty("BAIDU_KEY");
	/**
	 * 根据地址，获取坐标
	 * @param city
	 * @param area
	 * @param detail
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/location2Geo")
    @ResponseBody
    public JSONObject location2Geo(@RequestParam("city") String city,
    						   @RequestParam("area") String area,
    						   @RequestParam("detail") String detail,
    						   HttpServletRequest req) throws Exception {
        ResponseValue rv = new ResponseValue();
        
        StringBuffer address = new StringBuffer();
        if(city != null && !"".equals(city)) {
            city.trim();
            address.append(city);
        }
        if(area != null && !"".equals(area)) {
            area.trim();
            address.append(area);
        }
        if(detail != null && !"".equals(detail)) {
            detail = detail.replace(" ", "");
            address.append(detail);
        }
        
        JSONObject ret = BaiduUtil.geocodeByAddress(BAIDU_KEY, address.toString(), city);
        if(ret.containsKey("location") && ret.get("location") != null){
        	rv.setData(ret.get("location"));
        }
        System.out.println("根据地址获取坐标："+rv.toString());
        return JSONObject.parseObject(rv.toString());
    }
	
	/**
	 * 根据坐标获取地址信息
	 * @param prov
	 * @return
	 */
	@RequestMapping("/geo2Location")
    @ResponseBody
    public String geo2Location(@RequestParam("lat") double lat,
    						   @RequestParam("lng") double lng) {
		ResponseValue rv = new ResponseValue();
		
		JSONObject ret = BaiduUtil.geocodeByLocation(BAIDU_KEY, lat, lng);
		if(ret.containsKey("addressComponent") && ret.get("addressComponent") != null){
			rv.setData(ret.get("addressComponent"));
		}
		
		return rv.toString();
    }
	
}
